package tech.aistar.prj.controller;

import tech.aistar.prj.model.pojo.User;
import tech.aistar.prj.service.IUserService;
import tech.aistar.prj.service.impl.UserServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(urlPatterns = "/user/*")
public class LoginController extends HttpServlet {
    IUserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        req.setCharacterEncoding("utf-8");
//        ServletContext application = req.getServletContext();
//        Integer count = 0;
//        application.setAttribute("count",count);
        switch (pathInfo){
            case "/login":
                String username = req.getParameter("username");
                String password = req.getParameter("password");
//                String check = req.getParameter("check");

                User user = userService.findByUsername(username);
                if (null == user) {
//            System.out.println("用户不存在");

                    resp.sendRedirect("/servlet/nav/loginView?error=0");
                } else {
//            String name =  userService.findByUsername(username).getUsername();
                    String pwd = user.getPassword();
//            username.equals(name) &&
                    if (pwd.equals(password)) {
//                    System.out.println("登录成功");
                        HttpSession session = req.getSession();
                        session.setAttribute("user",user);

//                        count++;
//                        application.setAttribute("count",count);
//                        System.out.println(count);

//                        if (null!=check && check.length()>0){
//                            Cookie u = new Cookie(username,password);
//                            u.setPath("/");
//                            //设置过期时间 - s
//                            u.setMaxAge(60*10);//十分钟
//                            resp.addCookie(u);
//                        }else {
//                            Cookie c = new Cookie(username,null);
//                            c.setMaxAge(0);
//                            c.setPath("/");
//                            resp.addCookie(c);
//                        }

                        resp.sendRedirect("/servlet/video/index");
                    } else {
//                    System.out.println("密码错误");
                        resp.sendRedirect("/servlet/nav/loginView?error=1");
                    }
                }
                break;
            case "/exit":
                //1. 清空session中的数据 - 但是这个session还在
                req.getSession().invalidate();
//                count--;
//                application.setAttribute("count",count);
                //2. 重定向到登录界面
                resp.sendRedirect("/servlet/nav/loginView");
//                System.out.println(count);
                break;
        }
    }
}

